package net.orange.framework.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.web.BaseController;
import net.orange.architecture.web.SelectItemComparator;
import net.orange.framework.logic.IGroupProcess;
import net.orange.framework.logic.IMenuProcess;
import net.orange.framework.model.Group;
import net.orange.framework.model.Menu;

/**
 * The menu managed bean using in JSF MVC modal. it is include the web page
 * content and control the action and page forward direction in the web site.
 */
@ManagedBean(name="menu")
@SessionScoped
public class MenuController extends BaseController {

	/**
	 * The menu's code want to search
	 */
	private String sr_menu_code;

	/**
	 * The menu's name want to search
	 */
	private String sr_menu_name;

	/**
	 * The menu level selection.
	 */
	private List<SelectItem> levels;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#getClazz()
	 */
	protected Class getClazz() {
		return Menu.class;
	}

	/**
	 * @return Returns the sr_menu_code.
	 */
	public String getSr_menu_code() {
		return sr_menu_code;
	}

	/**
	 * @return Returns the sr_menu_name.
	 */
	public String getSr_menu_name() {
		return sr_menu_name;
	}

	/**
	 * @param sr_menu_code
	 *            The sr_menu_code to set.
	 */
	public void setSr_menu_code(String sr_menu_code) {
		this.sr_menu_code = sr_menu_code;
	}

	/**
	 * @param sr_menu_name
	 *            The sr_menu_name to set.
	 */
	public void setSr_menu_name(String sr_menu_name) {
		this.sr_menu_name = sr_menu_name;
	}

	/**
	 * @return Returns the group_pks.
	 */
	public String[] getGrouppks() {
		Menu menu = (Menu) content;

		if (menu == null || menu.getGroups() == null
				|| menu.getGroups().size() <= 0)
			return null;

		Iterator groups = menu.getGroups().iterator();
		String[] pks = new String[menu.getGroups().size()];

		for (int i = 0; groups.hasNext(); ++i) {
			Group group = (Group) groups.next();
			pks[i] = group.getPk() + "";
		}

		return pks;
	}

	/**
	 * @param grouppks
	 *            The group_pks to set.
	 */
	public void setGrouppks(String[] grouppks) throws Exception {
		Menu menu = (Menu) content;

		if (menu != null && grouppks != null && grouppks.length > 0) {
			IGroupProcess gprocess = (IGroupProcess) ProcessFactory
					.get(Group.class);

			menu.getGroups().clear();

			for (int i = 0; i < grouppks.length; i++) {
				String pk = grouppks[i];

				if (pk != null && pk.length() > 0) {
					Group group = (Group) gprocess.find(Long.parseLong(pk));
					if (group != null)
						menu.getGroups().add(group);
				}
			}
		}
	}

	/**
	 * @return Returns the parent_pk.
	 */
	public String getParent_menu_code() {
		Menu menu = (Menu) content;
		return (menu != null && menu.getParent() != null) ? menu.getParent()
				.getMenu_code() : null;
	}

	/**
	 * @param parent_pk
	 *            The parent_pk to set.
	 */
	public void setParent_menu_code(String parent_menu_code) throws Exception {
		Menu menu = (Menu) content;
		IMenuProcess process = (IMenuProcess) ProcessFactory.get(Menu.class);

		if (menu != null) {
			menu.setParent((Menu) process.find("menu_code", parent_menu_code));
		}
	}

	/**
	 * @return the menus
	 */
	public List<SelectItem> getMenus() throws Exception {
		List<SelectItem> list = new ArrayList<SelectItem>();
		list.add(new SelectItem("", ""));

		IMenuProcess process = (IMenuProcess) ProcessFactory.get(Menu.class);
		Collection menus = process.listAll();

		for (Object thismenu : menus.toArray()) {
			String code = ((Menu) thismenu).getMenu_code();
			String name = ((Menu) thismenu).getMenu_name();

			name = repeat("-", code.length() - 1) + name + " (" + code + ")";
			list.add(new SelectItem(code, name));
		}

		Collections.sort(list, new SelectItemComparator());
		return list;
	}

	/**
	 * Return the select item (menu level) for selection component.
	 * 
	 * @return The select item (menu level) for selection component.
	 */
	public List<SelectItem> getLevels() {
		if (levels == null) {
			levels = new ArrayList<SelectItem>();

			levels.add(new SelectItem(new Integer(1), "1"));
			levels.add(new SelectItem(new Integer(2), "2"));
			levels.add(new SelectItem(new Integer(3), "3"));
			levels.add(new SelectItem(new Integer(4), "4"));
		}
		return levels;
	}

	/**
	 * Repeat the string.
	 * 
	 * @param s
	 *            The original string.
	 * @param t
	 *            The repeat times.
	 * @return the string that repeat t times.
	 * @throws Exception
	 */
	private String repeat(String s, int t) throws Exception {
		String r = "";
		if (s != null)
			for (int i = 0; i < t; ++i)
				r += s;
		return r;
	}
}
